<template>
    <div class="box">
        <div class="crop">
            <div class="cropper">
                <VueCropper ref="cropper" :img="cropSource" :outputSize="option.size" :outputType="option.outputType"
                    :autoCrop="option.autoCrop" :fixedBox="false" :autoCropWidth="option.autoCropWidth"
                    :autoCropHeight="option.autoCropHeight" :full="option.full" @realTime="realTime">
                </VueCropper>
            </div>
            <div class="view">
                <div :class="{ 'img-preview': true, 'radian': radian }" :innerHTML="previewHtml"></div>
                <div class="btn">
                    <div class="item" title="下载截图" @click="DownImg">
                        <el-icon>
                            <Download />
                        </el-icon>
                    </div>
                    <div class="item" title="关闭" @click="$emits('cropClose')">
                        <el-icon>
                            <Back />
                        </el-icon>
                    </div>
                </div>
            </div>
            <div class="tool">
                <div class="item" title="矫正" @click="correct">
                    <el-icon>
                        <FullScreen />
                    </el-icon>
                </div>
                <div class="item" title="左旋" @click="cropper.rotateLeft()">
                    <el-icon>
                        <RefreshLeft />
                    </el-icon>
                </div>
                <div class="item" title="右旋" @click="cropper.rotateRight()">
                    <el-icon>
                        <RefreshRight />
                    </el-icon>
                </div>
                <div class="item" title="弧度" @click="arcChange">
                    <el-icon>
                        <Refresh />
                    </el-icon>
                </div>
            </div>
        </div>

    </div>
</template>
<script setup lang='ts'>
import { Back, Download, FullScreen, RefreshLeft, RefreshRight, Refresh } from '@element-plus/icons-vue'
import { ref, type Ref } from 'vue'
import 'vue-cropper/dist/index.css'
import { VueCropper } from "vue-cropper"
defineProps({
    cropSource: {
        type: String,
        required: true
    }
})
const $emits = defineEmits(['cropClose'])
const cropper: any = ref({})
//参数
const option = ref({
    size: 1,
    outputType: 'jpg',
    autoCrop: true,
    fixedNumber: [1, 1],
    autoCropWidth: 128,
    autoCropHeight: 128,
    full: true
})
const radian = ref(false)
//预览
const previewHtml: Ref<string> = ref('')
/**截图预览*/
const realTime = ((e: any) => {
    previewHtml.value = e.html
})
/**下载截图*/
const DownImg = () => {
    var aLink = document.createElement('a')
    aLink.download = Date.now().toString()

    cropper.value.getCropData((data: string) => {
        // 创建一个Image对象用于加载Base64格式图片
        var img = new Image()
        img.src = data

        img.onload = function (this: any) {
            // 获取图片的原始宽度和高度
            var originalWidth = this.width
            var originalHeight = this.height

            if (radian.value) {
                var canvas = document.createElement('canvas')
                var ctx = canvas.getContext('2d')
                canvas.width = option.value.full ? originalWidth : cropper.value.cropW
                canvas.height = option.value.full ? originalHeight : cropper.value.cropH
                ctx?.beginPath()
                ctx?.ellipse(
                    canvas.width / 2,
                    canvas.height / 2,
                    canvas.width / 2,
                    canvas.height / 2,
                    0,
                    0,
                    Math.PI * 2
                );
                ctx?.clip()
                ctx?.drawImage(img, 0, 0, canvas.width, canvas.height)
                aLink.href = canvas.toDataURL('image/png')
                aLink.click()
            } else {
                aLink.href = data
                aLink.click()
            }
        }
    })
}
const correct = () => {
    const size = Math.min(cropper.value.cropW, cropper.value.cropH)
    cropper.value.cropW = size
    cropper.value.cropH = size
}
const arcChange = () => {
    radian.value = !radian.value
}
</script>
<style lang="scss" scoped>
.box {
    .crop {
        width: 100%;
        height: 500px;
        display: grid;
        grid-template-columns: 1fr 300px;
        grid-gap: 10px;
        padding: 10px;

        .cropper {
            width: 100%;
            height: 100%;
            background-color: #cccccc2e;
            border: 1px solid #cccccc4d;
        }

        .view {
            width: 100%;
            height: 400px;
            display: flex;
            flex-direction: column;
            gap: 20px;

            .img-preview {
                width: 300px;
                height: 300px;
                overflow: auto;
                background-color: #cccccc2e;
                border: 1px solid #cccccc4d;
                display: flex;
                justify-content: center;
                align-items: center;
                @include mixin.scroll_bar();
            }

            .radian {
                :deep(.show-preview) {
                    border-radius: 50%;
                }
            }

            .btn {
                width: 100%;
                display: flex;
                justify-content: center;
                gap: 10px;

                .item {
                    height: 32px;
                    width: 32px;
                    margin: 0;
                    font-size: 20px;
                    box-shadow: 0px 0px 2px white;
                    border-radius: 50%;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    cursor: pointer;
                    color: white;
                    transition: all 0.5s;
                    box-shadow: 0.3em 0.3em 0.7em #00000015;
                    transition: border 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);

                    &:nth-child(1) {
                        background-color: #1961fe;
                    }

                    &:nth-child(2) {
                        background-color: #b0a8b9;
                    }

                    &:hover {
                        opacity: 0.8;
                    }
                }
            }
        }

        .tool {
            display: flex;
            gap: 10px;
            padding: 10px;
            justify-content: center;

            .item {
                height: 32px;
                width: 32px;
                margin: 0;
                font-size: 20px;
                box-shadow: 0px 0px 2px white;
                border-radius: 50%;
                display: flex;
                justify-content: center;
                align-items: center;
                cursor: pointer;
                color: white;
                transition: all 0.5s;
                box-shadow: 0.3em 0.3em 0.7em #00000015;
                transition: border 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);

                &:nth-child(1) {
                    background-color: #1961fe;
                }

                &:nth-child(2) {
                    background-color: #00d107;
                }

                &:nth-child(3) {
                    background-color: #ea5600;
                }

                &:nth-child(4) {
                    background-color: #8c00ea;
                }

                &:hover {
                    opacity: 0.8;
                }
            }
        }
    }


}
</style>
